package cn.dansj.common.utils.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public abstract class TelnetUtils {
    private static final Logger logger = LoggerFactory.getLogger(TelnetUtils.class);
    /**
     * 测试telnet 机器端口的连通性
     */
    public static boolean telnet(String hostname, int port, int timeout) {
        Socket socket = new Socket();
        boolean isConnected;
        try {
            socket.connect(new InetSocketAddress(hostname, port), timeout); // 建立连接
            isConnected = socket.isConnected(); // 通过现有方法查看连通状态
        } catch (IOException e) {
            isConnected = false;
        } finally {
            try {
                socket.close(); // 关闭连接
            } catch (IOException ignore) {
            }
        }
        logger.info("telnet " + hostname + ":" + port + " ==> isConnected: " + isConnected);
        return isConnected;
    }
}
